﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Namer.Pages
{
    public class StopNamePAge
    {
        private static readonly List<Control> PageControls = new List<Control>();
        public StopNamePAge() => Interlize();

        private static void OnLoad()
        {
            names.DoWork += Names_DoWork;
        }

        private static bool OnStop = false;

        private static void Names_DoWork(object sender, DoWorkEventArgs e)
        {
            string[] data = BackGround.UsersList[Class1.SelectedItem.ToString()];
            Random t1 = new Random();
            while (OnStop)
            {
                Name1.Text = data[t1.Next(0, data.Count())];
                Thread.Sleep(100);
            }
        }

        public List<Control> GetControls(int x)
        {
            PageControls.Clear();
            Interlize();
            foreach (Control ctr in PageControls) ctr.Location = new Point(x + ctr.Location.X, ctr.Location.Y);
            return PageControls;
        }
        private static UILabel Name1;
        private static UIComboBox Class1;
        private static void Interlize()
        {
            UIMarkLabel title = new UIMarkLabel()
            {
                Text = "暂停点名",
                Location = new Point(10, 10)
            }; PageControls.Add(title);
            Name1 = new UILabel()
            {
                Location = new Point(10, 120),
                Width = UI.MainForm.Width - 70,
                Height = 45,
                TextAlign = ContentAlignment.MiddleCenter,
                Font = new Font("Microsoft YaHei", 18),
                Text = "请点击按钮开始吧!"
            }; PageControls.Add(Name1);
            UIButton GenRandom = new UIButton()
            {
                Text = "开始",
                Style = UIStyle.LayuiGreen,
                Location = new Point(10, UI.MainForm.Height - 120),
                Size = new Size(UI.MainForm.Width - 70, 30)
            }; PageControls.Add(GenRandom); GenRandom.Click += GenRandom_Click;
            Class1 = new UIComboBox()
            {
                Size = new Size(UI.MainForm.Width - 70, 30),
                Location = new Point(10, UI.MainForm.Height - 80)
            }; PageControls.Add(Class1); intersize();
            OnLoad();
        }

        private static void intersize()
        {
            Class1.Items.Clear();
            foreach (string dara in BackGround.cm) Class1.Items.Add(dara);
            if (Class1.Items.Count > 0) Class1.SelectedIndex = 0;
        }

        private static void GenRandom_Click(object sender, EventArgs e)
        {
            UIButton Fbtn = (UIButton)sender;
            if (Fbtn.Name.Equals("stop"))
            {
                Fbtn.Name = "start";
                Fbtn.Style = UIStyle.LayuiGreen;
                Fbtn.Text = "开始";
                endName();
            }
            else
            {
                Fbtn.Name = "stop";
                Fbtn.Style = UIStyle.Red;
                Fbtn.Text = "停止";
                startName();
            }
        }

        private static BackgroundWorker names = new BackgroundWorker();
        private static void startName()
        {
            OnStop = true;
            names.RunWorkerAsync();
        }

        private static void endName()
        {
            OnStop = false;
        }

    }
}
